import * as cc from "cc";
import { resourcesMgr } from "../../../framework/loader/ResourcesMgr";
import { UIBase } from '../../../framework/ui/UIBase';
import { VMCenter } from '../../../framework/vm/VMCenter';
import { VMEntity } from '../../../framework/vm/VMEntity';
import { PlayerItemPackage, ConfigItemStorage } from "../../../userdata/StorageModel";
const { ccclass } = cc._decorator;

/**
 * Predefined variables
 * Name = CellEquipItem
 * DateTime = Sun Mar 20 2022 21:31:10 GMT+0800 (中国标准时间)
 * Author = 272493431
 * FileBasename = CellEquipItem.ts
 * FileBasenameNoExtension = CellEquipItem
 * URL = db://assets/scripts/scene/VMDemo/items/CellEquipItem.ts
 * ManualUrl = https://docs.cocos.com/creator/3.4/manual/zh/
 *
 */
 
interface CellData {
    idx: number;
    type:number; // 装备孔类型（0-武器 1-服装 2- 装饰）
    rank:number //品质星级 （通用）
    pic:number //图片编号 （通用）
    equip:0|1 //是否被装备（背包用）
}

@ccclass('CellEquipItem')
export class CellEquipItem extends UIBase implements IMVVM{
    _vmTag: string;
    _vmEntitys: VMEntity[];

    private _index : number = -1;
    set index(v: number){
        if(v<=-1) v =-1;

        let res =  this.getData(v);
        if(res == true){
            this._index = v; 
        }
    }
    get index(){
        return this._index;
    }


    data:CellData = {
        idx: -1,
        type:0, //装备孔的类型
        rank:0, //品质星级 （通用）
        pic:0,  //图片编号 （通用）
        equip:0 //是否被装备（背包用）
    }
    onLoaded(){
        VMCenter.add(this.data,this);

        let icon_test = this.find("icon_test");
        let star_list = this.find("star_list");
        let equip = this.find("equip");

        
        VMCenter.Sprite(this,icon_test,{watchPath: "*.pic",formator (opts) {
            return new Promise<cc.SpriteFrame>((resolve, reject) => {
                if (!opts.newValue) {
                    resolve(null);
                    return;
                }
                resourcesMgr.load("",`texture/equip/icon${opts.newValue}`,cc.SpriteFrame, (err, spriteFrame: cc.SpriteFrame) => {
                    if (err) {
                        return resolve(null);
                    }
                    resolve(spriteFrame);
                });
            });
        }})
    
        VMCenter.Children(this,star_list,{watchPath: "*.rank",formator(opts){
            return opts.newValue >= opts.nodeIdx;
        }});
        
        VMCenter.Node(this,equip,{watchPath: "*.equip",formator(opts){
            return !!opts.newValue;
        }});

        VMCenter.Node(this,this.node,{watchPath: "*.idx",formator(opts){
            return opts.newValue > -1;
        }});

    }

    onUnloaded(){

        VMCenter.remove(this);
    }

    //获取数据
    getData(index:number):boolean{

        let player =  PlayerItemPackage.find(v=> v.id == index);
        if(player ==null)return false;
        let item = ConfigItemStorage.find(v=>v.id == player.itemId );
        if(item == null)return false;

        this.data.idx = index;
        this.data.type = item.type;
        this.data.rank = item.rank;
        this.data.pic = item.pic;
        this.data.equip = player.equip as 0;
        
        // this.data = {
        //     idx: index,
        //     type:item.type, //装备孔的类型
        //     rank:item.rank, //品质星级 （通用）
        //     pic:item.pic,  //图片编号 （通用）
        //     equip:player.equip as 0 //是否被装备（背包用）
        // }
        return true;
    }
}
